<?php
/*
 * @version :  2013-4-22
 * @project :  account-system
 * @Author  :  Allen
 * @e-mail  :  shulei@maszy.cn
 */
//$arr["dns"]= array("dns_1"=>"14.137.150.75","dns_2"=>"69.175.118.42","dns_3"=>"8.8.8.8","dns_4"=>"8.8.4.4");
//$arr["SSID"]= array(
//				array("id"=>1,"name"=>"ipphone","password"=>"12345678","priority"=>1,"speedlimit"=>100),
//				array("id"=>2,"name"=>"iptv","password"=>"12345678","priority"=>2,"speedlimit"=>150),
//				array("id"=>3,"name"=>"work","password"=>"12345678","priority"=>3,"speedlimit"=>500),
//				array("id"=>4,"name"=>"home","password"=>"12345678","priority"=>4,"speedlimit"=>300),
//				array("id"=>5,"name"=>"","password"=>"","priority"=>0,"speedlimit"=>100),
//				array("id"=>6,"name"=>"","password"=>"","priority"=>0,"speedlimit"=>150),
//				array("id"=>7,"name"=>"","password"=>"","priority"=>0,"speedlimit"=>500),
//				array("id"=>8,"name"=>"","password"=>"","priority"=>0,"speedlimit"=>300),
//				);
//echo json_encode($arr);
 require_once (dirname(__FILE__)."/../config/config.php");

 $content = getXml();
 outPutXml($content);
function getXml(){
	$content = "";
	$content.= "<result>\n";
	$content.= "<dnslist>\n";
	$content.= "<dns>\n";
	if(isset($_REQUEST['mac'])){
		$sql="select * from zy_equipment  where MacAddress='".$_REQUEST['mac']."'";
		$rs = mysql_query($sql);
		$row = mysql_fetch_array($rs);
		$user_id=$row['updater'];
		$sqldns="select * from zy_dnslist where user_id='".$user_id."'";
		$rs1 = mysql_query($sqldns);
		$row1 = mysql_fetch_array($rs1);
	}else if(isset($_REQUEST['username'])){
		$username=$_REQUEST['username'];
		$password=$_REQUEST['password'];
		// 验证用户名是否存在
		$sql="select * from zy_user where user_name = '".$username."' and password = '".$password."'";
		$rs = mysql_query($sql);
		$row = mysql_fetch_array($rs);
		if($row==""){
			$message="login fail";
		    return $message;
		}
		$user_id=$row['user_id'];
		$sqldns="select * from zy_dnslist where user_id='".$user_id."'";
		$rs1 = mysql_query($sqldns);
		$row1 = mysql_fetch_array($rs1);
		
	}else{
		$message="login fail";
		 return $message;
	}
		if($row1==null)
		{
		//如果用户不包含DNS记录，则使用默认值
			$content.= "<dns_1>14.137.150.75</dns_1>\n";
			$content.= "<dns_2>69.175.118.42</dns_2>\n";
			$content.= "<dns_3>8.8.8.8</dns_3>\n";
			$content.= "<dns_4>8.8.4.4</dns_4>\n";
		}else{//如果用户包含DNS记录，则使用数据库的记录	
			$content.= "<dns_1>".$row1['dns1']."</dns_1>\n";
			$content.= "<dns_2>".$row1['dns2']."</dns_2>\n";
			$content.= "<dns_3>".$row1['dns3']."</dns_3>\n";
			$content.= "<dns_4>".$row1['dns4']."</dns_4>\n";
		}
		$content.= "</dns>\n";
		$content.= "</dnslist>\n";
		$sqluser = "select * from zy_routersettings where user_id='".$user_id."'";
		$rsult = mysql_query($sqluser);
		$content.= "<ssidlist>\n";
		while($rows = mysql_fetch_array($rsult)){
			$content.= "<ssid>\n";
			$content.= "<id>".$rows['id']."</id>\n";
			$content.= "<name>".$rows['name']."</name>\n";
			$content.= "<password>".$rows['password']."</password>\n";
			$content.= "<priority>".$rows['priority']."</priority>\n";
			$content.= "<speedlimit>".$rows['speedlimit']."</speedlimit>\n";
			$content.= "</ssid>\n";
		}
		$content.= "</ssidlist>\n";
		$content.= "</result>\n";
	return $content;
}




function outPutXml($content) {
	header("Content-type:text/xml");
	echo "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\" ?>\n";
	echo "<netcuberesult>\n";
	echo $content;
	echo "</netcuberesult>";
	die();
}
?>
